#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>
#include "UART.h"
#include "Lcd_Driver.h"
#include "FIFO.h"

/**
  * 函    数：串口初始化
  * 参    数：无
  * 返 回 值：无
  */
void Serial_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
 
    // 启用时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
 
    // 配置USART1 TX(PA9)和RX(PA10)引脚 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    // 配置USART1 参数 
    USART_InitStructure.USART_BaudRate =115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);
 
    // 配置中断优先级（改为USART1_IRQn）
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
    NVIC_Init(&NVIC_InitStructure); 
 
    // 使能USART1 接收中断 
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
 
    // 使能USART1
    USART_Cmd(USART1, ENABLE); 

}

/**
  * 函    数：串口发送一个字节
  * 参    数：Byte 要发送的一个字节
  * 返 回 值：无
  */
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);		
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
	
}


/**
  * 函    数：USART1中断函数
  */
extern volatile uint8_t frameReady;

void USART1_IRQHandler(void)
{

	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)		
	{
		if(USART_ReceiveData(USART1)==0x77)
	
		  frameReady=0;
		  
		  USART_ClearITPendingBit(USART1, USART_IT_RXNE);		//清除标志位  
			
	}
}


void USART_DMA(void)
	{
	Serial_Init();
	GPIO_EXTI();
	GPIO_EXTI1();
		
  }


void GPIO_EXTI(void)
{
	
	
	
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);

	  GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	
		EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource4);//PB4对中断线4
    	   	 
    EXTI_InitStructure.EXTI_Line=EXTI_Line4;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);		
    
    NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;			
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;	
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;					 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;								
    NVIC_Init(&NVIC_InitStructure);  	    
}

extern uint8_t mode;
void EXTI4_IRQHandler(void)
{		 		
	if(EXTI_GetITStatus(EXTI_Line4)==SET)	//是8线的中断
	{
		for(int i=0;i<99999;i++); //延迟消抖
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4)==0)
		{
			if(mode==0)
		  mode=1;
		else
			mode=0;
		
	  }
		
	EXTI_ClearITPendingBit(EXTI_Line4);  //清除EXTI8线路挂起位
	}		
}

void GPIO_EXTI1(void)
{
	
	
	
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE);

	  GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
	
		EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource14);//PB4对中断线4
    	   	 
    EXTI_InitStructure.EXTI_Line=EXTI_Line14;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);		
    
    NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;			
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;	
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;					 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;								
    NVIC_Init(&NVIC_InitStructure);  	    
}

extern uint8_t mode;
void EXTI15_10_IRQHandler(void)
{		 		
	if(EXTI_GetITStatus(EXTI_Line14)==SET)	//是8线的中断
	{
		for(int i=0;i<99999;i++){} //延迟消抖
				
			mode=2;

		
	EXTI_ClearITPendingBit(EXTI_Line14);  //清除EXTI8线路挂起位
	}		
}




